package com.ican.service.user;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ican.common.SystemConstants;
import com.ican.dao.user.ResourcesDao;
import com.ican.dao.user.RoleDao;
import com.ican.domain.user.Resources;
import com.ican.domain.user.Role;

@Service("resourcesService")
public class ResourcesService {

    private RoleDao roleDao;
    private ResourcesDao resourcesDao;

    public List<Resources> findAllResourceByUserId(Integer userId){
        List<Role> roles = roleDao.findRoleByUserId(userId);
        if(roles != null && roles.size() > 0){
            String roleIds = null;
            for(Role r : roles){
                roleIds = roleIds == null ? r.getId().toString() : roleIds + "," + r.getId().toString();
            }
            return resourcesDao.findResourceByUserIdAndRoleId(userId, roleIds);
        }else{
            return resourcesDao.findResourceByUserId(userId);
        }
    }
    
    public List<Resources> findResourceByUserId(Integer userId){
        return resourcesDao.findResourceByUserId(userId);
    }
    
    public List<Resources> findResourceByRoleId(Integer roleId){
        return resourcesDao.findResourceByRoleId(roleId);
    }
    
    public Resources findResourceById(Integer id){
        return resourcesDao.findResourceById(id);
    }
    
    /**
     * @param type    0:可用；1：不可用; 其他（-1）：全部
     */
    public List<Resources> getAllResource(int type){
        return resourcesDao.findAllResource(type);
    }
    
    public void changeResourceSort(Integer fromId, Integer fromPid, Integer toId, Integer toPid, String moveType){
        //"inner"：成为子节点，"prev"：成为同级前一个节点，"next"：成为同级后一个节点
        if("inner".equals(moveType)){
            resourcesDao.updateResourcesPid(fromId, toId);
            return;
        }
        if(!fromPid.equals(toPid)){
            resourcesDao.updateResourcesPid(fromId, toId);
        }
        List<Resources> rs = resourcesDao.findAllResourceByPid(toPid);
        Resources rFrom = null;
        for(Resources r : rs){
            if(r.getId().equals(fromId)){
                rFrom = r;
                break;
            }
        }
        int i = 0;
        for(Resources r : rs){
            if(!r.getId().equals(fromId) && !r.getId().equals(toId)){
                r.setSort(i);
                resourcesDao.save(r);
                i++;
            }else{
                if(r.getId().equals(toId)){
                    if("prev".equals(moveType)){
                        rFrom.setSort(i);
                        resourcesDao.save(r);
                        i++;
                        r.setSort(i);
                        resourcesDao.save(r);
                        i++;
                    }else{
                        r.setSort(i);
                        resourcesDao.save(r);
                        i++;
                        rFrom.setSort(i);
                        resourcesDao.save(r);
                        i++;
                    }
                }
            }
        }
    }
    
    public void updateResources(Resources resources){
        resources.setUrl(resources.getUrl() + "?" + SystemConstants.KEY_CATEGORY_ID + "=" + resources.getId());
        resourcesDao.updateResources(resources);
    }
    
    public void addResources(Resources resources){
        resourcesDao.addResources(resources);
        resources.setUrl(resources.getUrl() + "?" + SystemConstants.KEY_CATEGORY_ID + "=" + resources.getId());
        resourcesDao.updateResourcesUrl(resources.getId(), resources.getUrl());
    }
    
    @Resource
    public void setRoleDao(RoleDao roleDao) {
        this.roleDao = roleDao;
    }
    @Resource
    public void setResourcesDao(ResourcesDao resourcesDao) {
        this.resourcesDao = resourcesDao;
    }
}
